package com.glela.goods.dao.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.glela.goods.dao.InventoryDao;
import com.glela.goods.dao.vo.LimitSaleGoodsInventroy;
import com.glela.goods.mapper.InventoryMapper;
import com.glela.goods.model.Inventory;

/**
 * @author liujie
 * @2016年10月20日
 */
@Repository
public class InventoryDaoImpl implements InventoryDao {
	@Autowired
	private InventoryMapper inventoryMapper;

	@Override
	public Map<Long, Inventory> selectByIds(Collection<Long> inventoryIdSet) {
		List<Inventory> inventories = inventoryMapper.selectByIds(inventoryIdSet);
		Map<Long, Inventory> rsltMap = new HashMap<>();
		for (Inventory inventory : inventories) {
			rsltMap.put(inventory.getId(), inventory);
		}
		return rsltMap;
	}

	@Override
	public Inventory selectInventoryByGoodsStoreIdSpecInfo(long goodsId, int storeId, String specInfo) {
		return inventoryMapper.selectInventoryByGoodsStoreIdSpecInfo(goodsId, storeId, specInfo);
	}

	@Override
	public long selectLimitSaleInventoryByGoodsId(int limitTimeSaleId, long goodsId) {
		Long a = inventoryMapper.selectLimitSaleInventoryByGoodsId(limitTimeSaleId, goodsId);
		return a == null ? 0 : a;
	}

	@Override
	public LimitSaleGoodsInventroy selectLimitSaleInventoryByGoodsId(Map<Integer, Set<Long>> limitSaleGoodsIdMap) {
		List<Map<String, Object>> aList = inventoryMapper.selectLimitSaleInventoryByGoodsIds(limitSaleGoodsIdMap);
		LimitSaleGoodsInventroy limitSaleGoodsInventroy = new LimitSaleGoodsInventroy();
		for (Map<String, Object> map : aList) {
			if (MapUtils.isEmpty(map)) {
				continue;
			}
			int limitTimeSaleId = MapUtils.getIntValue(map, "limitTimeSaleId", -1);//分时抢购ID
			long goodsId = MapUtils.getLongValue(map, "goodsId", -1l);//商品ID
			if (limitTimeSaleId < 1 || goodsId < 1) {
				continue;
			}

			int inventoryNum = MapUtils.getIntValue(map, "inventoryNum", 0);
			int skuCount = MapUtils.getIntValue(map, "skuCount", 0);
			int showCount = MapUtils.getIntValue(map, "showCount", 0);

			if (inventoryNum > 0) {//实际库存大于0取显示库存和秒杀库存最小值
				int _nums = skuCount > showCount ? showCount : skuCount;

				int nums = limitSaleGoodsInventroy.getNums(limitTimeSaleId, goodsId);
				limitSaleGoodsInventroy.put(limitTimeSaleId, goodsId, nums + _nums);
			}
		}
		return limitSaleGoodsInventroy;
	}

	@Override
	public Inventory selectInventoryByProductId(long productId) {
		return inventoryMapper.selectInventoryByProductId(productId);
	}

	@Override
	public List<Inventory> selectByGoodsId(long goodsId) {
		return selectByGoodsId(goodsId, 1);
	}

	@Override
	public List<Inventory> selectByGoodsId(long goodsId, Integer status) {
		return inventoryMapper.selectByGoodsId(goodsId, status);
	}

	@Override
	public Map<Long, Set<Inventory>> selectByGoodsIds(Collection<Long> goodsIds, int status) {
		List<Inventory> a = inventoryMapper.selectByGoodsIds(goodsIds, status);
		Map<Long, Set<Inventory>> m = new HashMap<>();
		for (Inventory inventory : a) {
			if (inventory == null) {
				continue;
			}
			if (CollectionUtils.isEmpty(m.get(inventory.getGoodId()))) {
				m.put(inventory.getGoodId(), new HashSet<Inventory>());
			}
			m.get(inventory.getGoodId()).add(inventory);
		}
		return m;
	}

}
